package com.imoblife.brainwavestus.customview;


import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map.Entry;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.imoblife.brainwavestus.BrainWavestusApplication;
import com.imoblife.brainwavestus.ProductDetailActivity;
import com.imoblife.brainwavestus.R;
import com.imoblife.brainwavestus.customview.AbsProductList.OnItemListener;
import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.obpurchaseSystem.PriceManager;
import com.imoblife.brainwavestus.product.ProductInfo;
import com.imoblife.brainwavestus.product.Store;
import com.imoblife.brainwavestus.util.BitMapFlyweightFactory;
import com.imoblife.brainwavestus.util.DebugServerPushDataUtil;
import com.imoblife.brainwavestus.util.DebugUtil;
import com.imoblife.brainwavestus.util.ProductUtil;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

public class SpecialsProductsView extends FreatureProductsView {
	
	private Store mStore;
	
	public SpecialsProductsView(Activity activity) {
		super(activity);
		
		BrainWavestusApplication app
			= (BrainWavestusApplication) activity.getApplication();
		
		mStore = app.getStore();
	}

	protected void initializeProductsView() {

		mAbsProductList.setDataList(mStore.getDiscountProductNames());
		
		mAbsProductList.setOnItemClicker(new OnItemListener() {

			public void onItemClick(View view, String productId) {

				Intent intent = new Intent(mActivity, ProductDetailActivity.class);
				intent.putExtra(Constant.PRODUCT_ID, productId);
				mActivity.startActivity(intent);
			}

			public void onItemLoading(View parent, String productId) {

				String parentId = "";
				String temp = productId + ".music";
				String title = "";
			
				ProductInfo info = mStore.getProductByName(productId).getProductInfo();
				parentId = info.getParentId();
				title = info.getTitle();
				
				
				ImageView productIcon = (ImageView) parent
						.findViewById(R.id.product_icon);
				TextView productName = (TextView) parent
						.findViewById(R.id.product_name);
				ImageView icon = (ImageView) parent
						.findViewById(R.id.specials_price_icon);

				if (isXLargerScreen()) {

					LayoutParams params = productIcon.getLayoutParams();

					if (parent.getLayoutParams() != null) {
						params.width = parent.getLayoutParams().height
								- parent.getPaddingTop()
								- parent.getPaddingBottom();
						DebugUtil.debug(TAG, "w=" + params.width);
						DebugUtil.debug(TAG, "h=" + params.height);
						params.width *= 0.8;
						params.height = params.width;
						icon.setLayoutParams(params);
						productIcon.setLayoutParams(params);

					}
				}

				if (parentId != null && !parentId.equals("")) {
					
					productIcon.setImageBitmap(BitMapFlyweightFactory
							.getBitMapFlyweightFactory(mActivity.getAssets())
							.getBitMapFlyweight(parentId).getBitmap());
				
				} else {
			
					productIcon.setImageBitmap(BitMapFlyweightFactory
							.getBitMapFlyweightFactory(mActivity.getAssets())
							.getBitMapFlyweight(productId).getBitmap());

				}

				productName.setText(title);

				
				String discount = info.getDiscount();
				
				
				icon.setImageResource(ProductUtil.discount2ImgId(discount));

				TextView oldPrice = (TextView) parent
						.findViewById(R.id.odle_price);
				String priceOdle = PriceManager.getProductOdlePrice(mActivity,
						productId);

				oldPrice.setText(priceOdle);

				TextView newPrice = (TextView) parent
						.findViewById(R.id.new_price);
				newPrice.setText(PriceManager.getProductPrice(mActivity,
						productId, priceOdle));

			}
		});

	}

	protected void initalizeProductsNode() {

		mAbsProductList.setProductLayout(R.layout.tb_specials_product_node);

	}

	public void release() {

		super.release();
	}

}
